﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LimpaGaragem.DAO.Product
{
    public class Info
    {
        /// <summary>
        /// Get product
        /// </summary>
        public LimpaGaragem.Model.Product.Info GetProduct(int IDProduct)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_PRODUCT", IDProduct));

                //EXECUTE
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR008_SelectProduct", 0, objListOfParameters);
                LimpaGaragem.Model.Product.Info objInfo = null;
                if (objDataTable.Rows.Count > 0)
                {
                    objInfo = new LimpaGaragem.Model.Product.Info();
                    objInfo.ID = IDProduct;
                    objInfo.Name = objDataTable.Rows[0]["NM_PRODUCT"].ToString();
                    objInfo.Description = objDataTable.Rows[0]["DS_PRODUCT"].ToString();
                    objInfo.Price = Convert.ToDecimal(objDataTable.Rows[0]["NU_PRICE"]);
                    objInfo.IDUser = Convert.ToInt32(objDataTable.Rows[0]["ID_USR"]);
                    objInfo.IDTransactionType = Convert.ToInt32(objDataTable.Rows[0]["ID_TRANSACTION_TYPE"]);
                    objInfo.IDCategory = Convert.ToInt32(objDataTable.Rows[0]["ID_CATEGORY"]);
                    objInfo.IDStatus = Convert.ToInt32(objDataTable.Rows[0]["ID_STATUS"]);
                    switch (Convert.ToInt32(objDataTable.Rows[0]["ID_CONDITION"]))
                    {
                        case 1:
                            objInfo.Condition = LimpaGaragem.Model.Product.Info.EnumCondition.New;
                            break;
                        case 2:
                            objInfo.Condition = LimpaGaragem.Model.Product.Info.EnumCondition.Used;
                            break;
                    }

                    if (objDataTable.Rows[0]["ID_BRAND"] != DBNull.Value)
                        objInfo.IDBrand = Convert.ToInt32(objDataTable.Rows[0]["ID_BRAND"]);
                    if (objDataTable.Rows[0]["ID_MODEL"] != DBNull.Value)
                        objInfo.IDModel = Convert.ToInt32(objDataTable.Rows[0]["ID_MODEL"]);
                    if (objDataTable.Rows[0]["ID_VERSION"] != DBNull.Value)
                        objInfo.IDVersion = Convert.ToInt32(objDataTable.Rows[0]["ID_VERSION"]);
                   
                    objInfo.DateTimeInserted = Convert.ToDateTime(objDataTable.Rows[0]["DT_INC"]);
                    if (objDataTable.Rows[0]["DT_UPD"] != DBNull.Value)
                        objInfo.DateTimeInserted = Convert.ToDateTime(objDataTable.Rows[0]["DT_UPD"]);
                }
                return objInfo;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Insert new user
        /// </summary>
        /// <returns>ID USER</returns>
        public int InsertProduct(string Name,
                                string Description,
                                decimal Price,
                                int IDUser,
                                int IDTransactionType,
                                int IDCategory,
                                LimpaGaragem.Model.Product.Info.EnumCondition ECondition,
                                int? IDBrand,
                                int? IDModel,
                                int? IDVersion)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@NM_PRODUCT", Name));
                objListOfParameters.Add(new SqlParameter("@DS_PRODUCT", Description));
                objListOfParameters.Add(new SqlParameter("@NU_PRICE", Price));
                objListOfParameters.Add(new SqlParameter("@ID_USR", IDUser));
                objListOfParameters.Add(new SqlParameter("@ID_TRANSACTION_TYPE", IDTransactionType));
                objListOfParameters.Add(new SqlParameter("@ID_CATEGORY", IDCategory));
                objListOfParameters.Add(new SqlParameter("@ID_CONDITION", ECondition.GetHashCode()));
            
                if (IDBrand.HasValue)
                    objListOfParameters.Add(new SqlParameter("@ID_BRAND", IDBrand));
                else
                    objListOfParameters.Add(new SqlParameter("@ID_BRAND", DBNull.Value));
                if (IDModel.HasValue)
                    objListOfParameters.Add(new SqlParameter("@ID_MODEL", IDModel));
                else
                    objListOfParameters.Add(new SqlParameter("@ID_MODEL", DBNull.Value));
                if (IDVersion.HasValue)
                    objListOfParameters.Add(new SqlParameter("@ID_VERSION", IDVersion));
                else
                    objListOfParameters.Add(new SqlParameter("@ID_VERSION", DBNull.Value));

                //EXECUTE
                return objDataAccess.ExecuteQueryAndReturnValue(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR008_InsertProduct", 0, objListOfParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Alter product
        /// </summary>
        public void AlterProduct(int IDProduct,
                                string Name,
                                string Description,
                                decimal Price,
                                int IDUser,
                                int IDTransactionType,
                                int IDCategory,
                                LimpaGaragem.Model.Product.Info.EnumCondition ECondition,
                                int? IDBrand,
                                int? IDModel,
                                int? IDVersion)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_PRODUCT", IDProduct));
                objListOfParameters.Add(new SqlParameter("@NM_PRODUCT", Name));
                objListOfParameters.Add(new SqlParameter("@DS_PRODUCT", Description));
                objListOfParameters.Add(new SqlParameter("@NU_PRICE", Price));
                objListOfParameters.Add(new SqlParameter("@ID_USR", IDUser));
                objListOfParameters.Add(new SqlParameter("@ID_TRANSACTION_TYPE", IDTransactionType));
                objListOfParameters.Add(new SqlParameter("@ID_CATEGORY", IDCategory));
                objListOfParameters.Add(new SqlParameter("@ID_CONDITION", ECondition.GetHashCode()));
            
                if (IDBrand.HasValue)
                    objListOfParameters.Add(new SqlParameter("@ID_BRAND", IDBrand));
                else
                    objListOfParameters.Add(new SqlParameter("@ID_BRAND", DBNull.Value));
                if (IDModel.HasValue)
                    objListOfParameters.Add(new SqlParameter("@ID_MODEL", IDModel));
                else
                    objListOfParameters.Add(new SqlParameter("@ID_MODEL", DBNull.Value));
                if (IDVersion.HasValue)
                    objListOfParameters.Add(new SqlParameter("@ID_VERSION", IDVersion));
                else
                    objListOfParameters.Add(new SqlParameter("@ID_VERSION", DBNull.Value));

                //EXECUTE
                objDataAccess.ExecuteQuery(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR008_AlterProduct", 0, objListOfParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Delete product
        /// </summary>
        public void DeleteProduct(int IDProduct)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_PRODUCT", IDProduct));

                //EXECUTE
                objDataAccess.ExecuteQuery(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR008_DeleteProduct", 0, objListOfParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Alter password of user
        /// </summary>
        public void AlterProductStatus(int IDUser, int IDStatus)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_PRODUCT", IDUser));
                objListOfParameters.Add(new SqlParameter("@ID_STATUS", IDStatus));

                //EXECUTE
                objDataAccess.ExecuteQuery(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR008_AlterStatusProduct", 0, objListOfParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
